'use strict';

const Service = require('egg').Service;

class DashboardService extends Service {
  async tablestatus() {
    const rid = this.ctx.request.headers['rid'];  
    const unusedtable = await this.app.mysql.query('SELECT COUNT(1) b FROM restaurant_area \
    INNER JOIN area_table ON restaurant_area.aid = area_table.aid \
    INNER JOIN tablez ON tablez.id = area_table.tid \
    WHERE restaurant_area.rid = ? \
    AND tablez.`using` = 0',[rid]);

    const totaltable = await this.app.mysql.query('SELECT COUNT(1) t FROM area_table\
    WHERE area_table.aid IN \
    (SELECT restaurant_area.aid FROM restaurant_area WHERE restaurant_area.rid = ?)',[rid]);

    return {
        total:totaltable[0].t,
        unused:unusedtable[0].b
    }
  }
}

module.exports = DashboardService;
